package _interview100;

import java.util.ArrayList;
import java.util.List;

/**
 * 131. 分割回文串
 */
public class No131 {
    private List<List<String>> ans;
    private List<String> path;
    private String s;

    public List<List<String>> partition(String s) {
        ans = new ArrayList<>();
        path = new ArrayList<>();
        this.s = s;
        dfs(0);

        return ans;
    }

    private void dfs(int i) {
        if (i == s.length()) {
            ans.add(new ArrayList<>(path));
            return;
        }

        for (int j = i; j < s.length(); j++) {
            if (same(i, j)) {
                path.add(s.substring(i, j + 1));
                dfs(j + 1);
                path.remove(path.size() - 1);
            }
        }
    }

    private boolean same(int l, int r) {
        while (l <= r) {
            if (s.charAt(l++) != s.charAt(r--)) return false;
        }
        return true;
    }
}
